package ge.modules.events;

import java.util.ArrayList;

import ge.engine.Log;
import ge.lowlevel.EventEngine;
import ge.lowlevel.Vector;
import ge.modules.Module;
import ge.modules.logics.GeomCollideFigure;

/**
 * 
 * @author Abel
 *	Listens to the Collision Event
 */
public class CollisionListener implements Module 
{
	private GeomCollideFigure figure;
	private ArrayList<Vector> collisions;
	/**
	 * 	make a test collide with that figure.
	 * @param figure
	 */
	public CollisionListener(GeomCollideFigure figure)
	{
		this.figure = figure;
		collisions = null;
		 
	}
	/**
	 * 	Calls collide with id 0.
	 * @return ArrayList of Vector
	 */
	public ArrayList<Vector> collide()
	{
		 
		return collide(0);
	}
	/**
	 * 	Test the figure of the Listener to the other figures with the specified id. 
	 * @param id Select id of the figures to be tested. Value 0 means all of them.
	 * @return	ArrayList of Vector 
	 */
	public ArrayList<Vector> collide(int id)
	{
		if(collisions == null || id != 0)
		{
		 
			collisions = new ArrayList<Vector>();
			CollisionModule cm = (CollisionModule) EventEngine.get().ask("collide");
			if(cm == null)
				Log.write("Collide null");
			if(cm != null)
				collisions = cm.collide(figure,id);
		}
		return collisions;
	}
	/**
	 * 	
	 * @return True if the object is colliding with another one
	 */
	public boolean isCollision()
	{
		return collide().size() > 0;
	}
	/**
	 * 
	 * @return The number of elements we are colliding now.
	 */
	public int numOfCollisions()
	{
		return collide().size();
	}
	/**
	 * 	
	 * @return True if there is at least one collision on the way top
	 */
	public boolean collideUp()
	{
		ArrayList<Vector> vv = collide();
		for(Vector v : vv)
		{
			if(v.collideUp())
				return true;
		}
		return false;
	}
	/**
	 * 	
	 * @return True if there is at least one collision on the way down
	 */
	public boolean collideDown()
	{
		ArrayList<Vector> vv = collide();
		for(Vector v : vv)
		{
			if(v.collideDown())
				return true;
		}
		return false;
	}
	/**
	 * 	
	 * @return True if there is at least one collision on the way left
	 */
	public boolean collideLeft()
	{
		ArrayList<Vector> vv = collide();
		for(Vector v : vv)
		{
			if(v.collideLeft())
				return true;
		}
		return false;
	}
	/**
	 * 	
	 * @return True if there is at least one collision on the way right
	 */
	public boolean collideRight()
	{
		ArrayList<Vector> vv = collide();
		for(Vector v : vv)
		{
			if(v.collideRight())
				return true;
		}
		return false;
	}
	/**
	 * 	
	 * @return True if there is at least one inside its (or it inside the other one).
	 */
	public boolean collideInside()
	{
		ArrayList<Vector> vv = collide();
		for(Vector v : vv)
		{
			if(v.isNeutral())
				return true;
		}
		return false;
	}
	
	/**
	 * @param id 
	 * @return True if there is at least one collision object with that id
	 */
	public boolean isId(int id)
	{
		ArrayList<Vector> vv = collide(id);
		return vv.size() > 0;
	}
	@Override
	public void update() {
		collisions = null;
		
	}

	@Override
	public void draw() {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void delete() {
		// TODO Auto-generated method stub
		
	}
 
	
}
